import Vue from 'vue'
import Vuex from 'vuex'
import cart from './modules/cart'
import products from './modules/products'

Vue.use(Vuex)

const persist = (store) => {
  store.subscribe((mutation, state) => {
    localStorage.setItem('vuex-persist', JSON.stringify(state))
  })
}

const store = new Vuex.Store({
  state: {},
  mutations: {},
  actions: {},
  modules: {
    cart,
    products
  },
  plugins: [persist]
})

const state = JSON.parse(localStorage.getItem('vuex-persist'))

state && store.replaceState(state)

export default store
